import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Login',
      component: Login,
      meta: {
        title: '登录',
        icon: "icon-Home",
      },
    },
    
    {
      path: '/container',
      name: 'container',
      component: ()=>import('../views/container.vue'),
      // redirect: '/container/home',
      // children:[{
      //   path: 'convert',
      //   name: 'convert',
      //   component: ()=>import('../views/convert.vue')
      // }]
    },
    
  ]
})
let hasAddedRoutes = false; // 标记是否已经添加过子路由

// router.beforeEach((to, form, next) => {
//   const token = sessionStorage.getItem('token')
//   const routes = JSON.parse(sessionStorage.getItem('routerList')) || []
//   if (to.path === '/' || to.path === '/register') {
//     document.title = to.meta.title
//     next()
//   } else {
//     if (token) {
//       if (token) {
//         next()
//         return
//       }
//       const childrenRoutes = []; // 定义一个空数组存放子路由信息

//       routes.forEach((value) => {
        
//         let routeInfo = {
//           path: value.url,
//           name: value.name,
//           component: () => import(`../views/${value.component}.vue`)
//         }

//         childrenRoutes.push(routeInfo); // 将子路由信息添加到数组中

//       })

//       const existingRoutes = router.getRoutes()

//       const containerRoute = existingRoutes.find(route => route.name === 'container');
//       if (containerRoute && containerRoute.children) {
//         // 将需要动态添加的子路由信息添加到该数组中
//         containerRoute.children.push(...childrenRoutes);

//         // 动态添加更新后的路由
//         router.addRoute(containerRoute);

//         hasAddedRoutes = true;
//         next({ ...to, replace: true });
//       }
//     } else {
//       next("/");
//     }
//   }
// })
router.beforeEach((to,form,next)=>{
  const token = sessionStorage.getItem('token')
  const routes = JSON.parse(sessionStorage.getItem('routerList')) 
  if(to.path === '/' || to.path==='/register'){
    document.title = to.meta.title 
    next()
  }else{
    if(token){
      if(hasAddedRoutes){
        next()
        return
      }
      const childrenRoutes:any = []; // 定义一个空数组存放子路由信息
      routes.forEach((value:any)=>{
        let modules = import.meta.glob('../views/*.vue') 
        
      const comp = modules[`../views/${value.component}.vue`]
        let routeInfo={
          path:value.url,
          name:value.name,
          component:comp
        }
        if(value.children){
          routeInfo.children=value.children.map((item:any)=>{
            return {
              path:item.url,
              name:item.name,
              component:()=>import(`../views/${item.component}.vue`)
            }
          })
        }
        childrenRoutes.push(routeInfo); // 将子路由信息添加到数组中
        console.log(childrenRoutes);
      })

      const existingRoutes = router.getRoutes()

      const containerRoute = existingRoutes.find(route => route.name === 'container');
      if (containerRoute && containerRoute.children) {
      
        containerRoute.children.push(...childrenRoutes);

        // 动态添加更新后的路由
        router.addRoute(containerRoute);

        hasAddedRoutes = true;
        next({ ...to, replace: true });
      }
    }else {
      next("/");
    }
  }
})
export default router
